package com.imcrl.effect
{
	import starling.animation.IAnimatable;
	import starling.core.Starling;
	import starling.display.DisplayObject;
	
	public class ShakeEffect implements IAnimatable
	{
		private var offset:Number;
		private var list:Vector.<DisplayObject>;
		private var i:Number=0;
		public function ShakeEffect(offset:uint = 3)
		{
			list=new Vector.<DisplayObject>();
			
			this.offset=(Math.PI/180)*offset;
		}
		
		public function start():void{
			Starling.current.juggler.add(this);
		}
		
		public function stop():void{
			Starling.current.juggler.remove(this);
		}	
		
		public function clearAndStop():void{
			for each(var item:DisplayObject in list){
				item.rotation=0;
			}
			list.length=0;
			stop();
		}
		
		public function remove(item:DisplayObject):Boolean{
			var index:int=list.indexOf(item);
			if(index==-1){
				return false;
			}
			item.rotation=0;
			list.splice(index,1);
			return true;
		}
		
		public function add(item:DisplayObject):Boolean{
			if(list.indexOf(item)==-1){
				list.push(item);
				return true;
			}
			return false
		}
		
		public function advanceTime(time:Number):void
		{
			i+=0.5;
			for each(var item:DisplayObject in list){
				item.rotation=Math.cos(i)*offset;;
			}
			
		}
	}
}